package com.sinosoft.cnmuseum.servlets;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.sinosoft.cnmuseum.dao.MbMusicDbrwDao;
import com.sinosoft.cnmuseum.dao.MbMusicGcxxDao;
import com.sinosoft.cnmuseum.dao.MbMusicMultimediaDao;
import com.sinosoft.cnmuseum.dao.MbMusicTrackmessageDao;
import com.sinosoft.cnmuseum.dao.MbMusicWhrlxxxDao;
import com.sinosoft.cnmuseum.dao.MbMusicWhsxDao;
import com.sinosoft.cnmuseum.dao.MbMusicXzxxDao;
import com.sinosoft.cnmuseum.dao.MbMusicShapeDao;
import com.sinosoft.cnmuseum.dao.MbMusicSoundDao;
import com.sinosoft.cnmuseum.dao.MbMusicYxxxDao;
import com.sinosoft.cnmuseum.entity.MbGyByfl;
import com.sinosoft.cnmuseum.entity.MbGyCz;
import com.sinosoft.cnmuseum.entity.MbGyHsfl;
import com.sinosoft.cnmuseum.entity.MbGyMjfl;
import com.sinosoft.cnmuseum.entity.MbGyYqzl;
import com.sinosoft.cnmuseum.entity.MbGyYy;
import com.sinosoft.cnmuseum.entity.MbGyZq;
import com.sinosoft.cnmuseum.entity.MbXtglYhxx;
import com.sinosoft.cnmuseum.utils.Status;
import com.sinosoft.cnmuseum.utils.StringUtils;
import com.sinosoft.commontools.annotated.rquestMethod;
import com.sinosoft.commontools.annotated.rquestMethod.ReqMode;
import com.sinosoft.commontools.record.Record;

public class DataServlet {
	
	private static Logger logger = Logger.getLogger(DataServlet.class);
	
	@rquestMethod(MethodNote = ReqMode.All)
	public Record getGcxxData(HttpServletRequest request, HttpServletResponse response, Record data){
		Record record = new Record();
		record.add(Status.SATUS, Status.OK);
		try {
			MbMusicGcxxDao dao = new MbMusicGcxxDao();
			Record obj = dao.getById(data.getIntValue("id"));
			
			String[] baseTables = {MbGyHsfl.MB_GY_HSFL_TABLE_NAME,MbGyMjfl.MB_GY_MJFL_TABLE_NAME,MbGyByfl.MB_GY_BYFL_TABLE_NAME,
					MbGyYy.MB_GY_YY_TABLE_NAME,MbGyZq.MB_GY_ZQ_TABLE_NAME};
	        String[] fields = {"class_huosa","class_minjian","class_bayin","language","useNation"};
	        MbXtglBaseTableServlet baseTableServlet = new MbXtglBaseTableServlet();
	        for(int i=0;i<fields.length;i++){
	          String f=fields[i];
	          String value = obj.getStrValue(f);
	          String text = "";
	          if(StringUtils.isNoEmpty(value)){
	            text = baseTableServlet.getNameById(baseTables[i],Integer.valueOf(value));
	          }
	          obj.add(f+"_text", text);
	        }
	        
			record.add(Status.DATA, obj);
		} catch (Exception e) {
		  e.printStackTrace();
			record.add(Status.SATUS, Status.FAILED);
			record.add(Status.MSG, "数据异常！");
		}
		return record;
	}

	@rquestMethod(MethodNote = ReqMode.All)
	public Record getXzxxData(HttpServletRequest request, HttpServletResponse response, Record data){
		Record record = new Record();
		record.add(Status.SATUS, Status.OK);
		try {
			MbMusicXzxxDao dao = new MbMusicXzxxDao();
			Record obj = dao.getDataByGcxxid(data.getIntValue("id"));
			String[] baseTables = {MbGyCz.MB_GY_CZ_TABLE_NAME};
	        String[] fields = {"material"};
	        MbXtglBaseTableServlet baseTableServlet = new MbXtglBaseTableServlet();
	        for(int i=0;i<fields.length;i++){
	          String f=fields[i];
	          String value = obj.getStrValue(f);
	          String text = "";
				if (StringUtils.isNoEmpty(value)) {
					//字段多选情况遍历数组
					String[] singleIds = value.split(",");
					for (int j = 0; j < singleIds.length; j++) {
						String subText = "";
						try {
							subText = baseTableServlet.getNameById(baseTables[i], Integer.valueOf(singleIds[j]));
						} catch (Exception e) {
							subText = singleIds[j]; 
						}
						if (j != singleIds.length - 1) {
							text = text + subText + ",";
						} else {
							text = text + subText;
						}
					}
				}
	          obj.add(f+"_text", text);
	        }
			record.add(Status.DATA, obj);
		} catch (Exception e) {
		  e.printStackTrace();
			record.add(Status.SATUS, Status.FAILED);
			record.add(Status.MSG, "数据异常！");
		}
		return record;
	}

	@rquestMethod(MethodNote = ReqMode.All)
	public Record getYxxxData(HttpServletRequest request, HttpServletResponse response, Record data){
		Record record = new Record();
		record.add(Status.SATUS, Status.OK);
		try {
			MbMusicYxxxDao dao = new MbMusicYxxxDao();
			Record obj = dao.getDataByGcxxid(data.getIntValue("id"));
			record.add(Status.DATA, obj);
		} catch (Exception e) {
		  e.printStackTrace();
			record.add(Status.SATUS, Status.FAILED);
			record.add(Status.MSG, "数据异常！");
		}
		return record;
	}

	@rquestMethod(MethodNote = ReqMode.All)
	public Record getWhrlxxxData(HttpServletRequest request, HttpServletResponse response, Record data){
		Record record = new Record();
		record.add(Status.SATUS, Status.OK);
		try {
			MbMusicWhrlxxxDao dao = new MbMusicWhrlxxxDao();
			Record obj = dao.getDataByGcxxid(data.getIntValue("id"));
			record.add(Status.DATA, obj);
		} catch (Exception e) {
		  e.printStackTrace();
			record.add(Status.SATUS, Status.FAILED);
			record.add(Status.MSG, "数据异常！");
		}
		return record;
	}
	

	@rquestMethod(MethodNote = ReqMode.All)
	public Record getWhsxList(HttpServletRequest request, HttpServletResponse response, Record data){
		Record record = new Record();
		record.add(Status.SATUS, Status.OK);
		try {
			MbMusicWhsxDao dao = new MbMusicWhsxDao();
//			Record obj = dao.getById(data.getIntValue("id"));
			List<Record> list = dao.getDataByGcxxid(data.getIntValue("gcxxid"));
			String[] baseTables = {MbGyYqzl.MB_GY_YQZL_TABLE_NAME};
			String[] fields = {"instrumentType"};
			MbXtglBaseTableServlet baseTableServlet = new MbXtglBaseTableServlet();
			if(list!=null&&list.size()>0){
				for(Record whsx:list){
			        for(int i=0;i<fields.length;i++){
			          String f=fields[i];
			          String value = whsx.getStrValue(f);
			          String text = "";
			          if(StringUtils.isNoEmpty(value)){
			            text = baseTableServlet.getNameById(baseTables[i],Integer.valueOf(value));
			          }
			          whsx.add(f+"_text", text);
			        }
				}
			}
			record.add(Status.ROWS, list);
			record.add(Status.SATUS, Status.OK);
		} catch (Exception e) {
		  e.printStackTrace();
			record.add(Status.SATUS, Status.FAILED);
			record.add(Status.MSG, "数据异常！");
		}
		return record;
	}
	
	@rquestMethod(MethodNote = ReqMode.All)
	public Record getWhsxData(HttpServletRequest request, HttpServletResponse response, Record data){
		Record record = new Record();
		record.add(Status.SATUS, Status.OK);
		try {
			MbMusicWhsxDao dao = new MbMusicWhsxDao();
			Record obj = dao.getById(data.getIntValue("id"));
			record.add(Status.DATA, obj);
		} catch (Exception e) {
		  e.printStackTrace();
			record.add(Status.SATUS, Status.FAILED);
			record.add(Status.MSG, "数据异常！");
		}
		return record;
	}

	@rquestMethod(MethodNote = ReqMode.All)
	public Record getDbrwList(HttpServletRequest request, HttpServletResponse response, Record data){
		Record record = new Record();
		record.add(Status.SATUS, Status.OK);
		try {
			MbMusicDbrwDao dao = new MbMusicDbrwDao();
			List<Record> obj = dao.getDataByGcxxid(data.getIntValue("gcxxid"));
			record.add(Status.ROWS, obj);
			record.add(Status.SATUS, Status.OK);
		} catch (Exception e) {
		  e.printStackTrace();
			record.add(Status.SATUS, Status.FAILED);
			record.add(Status.MSG, "数据异常！");
		}
		return record;
	}
	@rquestMethod(MethodNote = ReqMode.All)
	public Record getDbrwData(HttpServletRequest request, HttpServletResponse response, Record data){
		Record record = new Record();
		record.add(Status.SATUS, Status.OK);
		try {
			MbMusicDbrwDao dao = new MbMusicDbrwDao();
			Record obj = dao.getById(data.getIntValue("id"));
			record.add(Status.DATA, obj);
		} catch (Exception e) {
		  e.printStackTrace();
			record.add(Status.SATUS, Status.FAILED);
			record.add(Status.MSG, "数据异常！");
		}
		return record;
	}
	
	//形制
	@rquestMethod(MethodNote = ReqMode.All)
	public Record getXzxxList(HttpServletRequest request, HttpServletResponse response, Record data){
		Record record = new Record();
		record.add(Status.SATUS, Status.OK);
		try {
			MbMusicShapeDao dao = new MbMusicShapeDao();
			List<Record> obj = dao.getDataByGcxxid(data.getIntValue("gcxxid"));
			record.add(Status.ROWS, obj);
			record.add(Status.SATUS, Status.OK);
		} catch (Exception e) {
		  e.printStackTrace();
			record.add(Status.SATUS, Status.FAILED);
			record.add(Status.MSG, "数据异常！");
		}
		return record;
	}
	
	//音响
	@rquestMethod(MethodNote = ReqMode.All)
	public Record getYXxxList(HttpServletRequest request, HttpServletResponse response, Record data){
		Record record = new Record();
		record.add(Status.SATUS, Status.OK);
		try {
			MbMusicSoundDao dao = new MbMusicSoundDao();
			List<Record> obj = dao.getDataByGcxxid(data.getIntValue("gcxxid"));
			record.add(Status.ROWS, obj);
			record.add(Status.SATUS, Status.OK);
		} catch (Exception e) {
		  e.printStackTrace();
			record.add(Status.SATUS, Status.FAILED);
			record.add(Status.MSG, "数据异常！");
		}
		return record;
	}
	
	//多媒体
	@rquestMethod(MethodNote = ReqMode.All)
	public Record getMultimediaList(HttpServletRequest request, HttpServletResponse response, Record data){
		Record record = new Record();
		record.add(Status.SATUS, Status.OK);
		try {
			MbMusicMultimediaDao dao = new MbMusicMultimediaDao();
			List<Record> obj = dao.getDataByGcxxid(data.getIntValue("gcxxid"));
			record.add(Status.ROWS, obj);
			record.add(Status.SATUS, Status.OK);
		} catch (Exception e) {
		  e.printStackTrace();
			record.add(Status.SATUS, Status.FAILED);
			record.add(Status.MSG, "数据异常！");
		}
		return record;
	}
	
	//曲目
	@rquestMethod(MethodNote = ReqMode.All)
	public Record getTrackmessageList(HttpServletRequest request, HttpServletResponse response, Record data){
		Record record = new Record();
		record.add(Status.SATUS, Status.OK);
		try {
			MbMusicTrackmessageDao dao = new MbMusicTrackmessageDao();
			List<Record> obj = dao.getDataByGcxxid(data.getIntValue("gcxxid"));
			record.add(Status.ROWS, obj);
			record.add(Status.SATUS, Status.OK);
		} catch (Exception e) {
		  e.printStackTrace();
			record.add(Status.SATUS, Status.FAILED);
			record.add(Status.MSG, "数据异常！");
		}
		return record;
	}
	
	

	

}
